import fetch from 'node-fetch';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))

async function scrapData() {
    let fileCount = 1;

    const firstPage = await fetch('https://explorer.test.siberium.net/txs?block_number=6306091&index=0&type=JSON&page_number=1', {
        headers: {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
        }
    });

        const jsonObjects = await firstPage.json();


    const pages = jsonObjects.next_page_params.pages_limit;

    const items = jsonObjects.items;

    for(let i = 0; i < items.length; i++ ) {
        if(items[i].includes('tile-type-token-transfer')) {
            console.info(fileCount)
             await fs.promises.writeFile(path.join(path.dirname(fileURLToPath(import.meta.url)), `${fileCount}.txt`) , items[i]);
            fileCount++;
        }
    }

    for(let pageNum = 2; pageNum <= pages; pageNum++ ) {
        const link = `https://explorer.test.siberium.net/txs?block_number=6306091&index=0&page_number=${pageNum}&page_size=50&pages_limit=${pages}&type=JSON`;
        console.info('страница: ' , pageNum);
        console.info(link);

        const pageData = await fetch(link, {
            headers: {
                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
            }
        })
        const pageJson= await pageData.json();

        const pageItems = pageJson.items

        for(let i = 0; i < pageItems.length; i++ ) {
            if(pageItems[i].includes('tile-type-token-transfer')) {
                console.info(fileCount)
                await fs.promises.writeFile(path.join(path.dirname(fileURLToPath(import.meta.url)), `${fileCount}.txt`) , pageItems[i]);
                fileCount++
            }
        }
        await sleep(1000)
    }
}

scrapData()

